package com.application.lucky.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.InviteBean;
import com.application.lucky.presenter.RegisterPresenter;
import com.application.lucky.view.RegisterView;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.mvp.BaseMvpActivity;
import com.application.mylibrary.utils.ToastUtils;
import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppInstallAdapter;
import com.fm.openinstall.model.AppData;
import com.google.gson.Gson;
import com.qunar.im.ui.util.StatusBarUtil;
import com.rengwuxian.materialedittext.MaterialEditText;

import static com.application.lucky.config.errorcode.LoginErrorCode.BIND_EXISTS_ERROR;
import static com.application.lucky.config.errorcode.LoginErrorCode.LOGIN_ERROR;
import static com.application.lucky.config.errorcode.LoginErrorCode.LOGIN_OR_PASSWORD_ERROR;

public class RegisterActivity extends BaseMvpActivity<RegisterView, RegisterPresenter>
        implements RegisterView, View.OnClickListener {

    private MaterialEditText etUsername;
    private MaterialEditText etNum;
    private TextView tvNum;
    private MaterialEditText etPassword;
    private MaterialEditText etPassword2;
    private View btRegister;
    private TitlebarView titleBar;
    private MaterialEditText etINviteCode;

    @Override
    public RegisterView createView() {
        return this;
    }

    @Override
    public RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        if (getPresenter() != null) {
            getPresenter().setNUm();
        }

        OpenInstall.getInstall(new AppInstallAdapter() {
            @Override
            public void onInstall(AppData appData) {
                String bindData = appData.getData();
                if (!TextUtils.isEmpty(bindData)) {
                    Gson gson = new Gson();
                    InviteBean bean = gson.fromJson(bindData, InviteBean.class);
                    if (bean != null) {
                        etINviteCode.setText(bean.userId);
                    }
                }
            }
        });
    }

    private void initListener() {
        tvNum.setOnClickListener(this);
        btRegister.setOnClickListener(this);
        titleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                startActivityAnim(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();

            }

            @Override
            public void rightClick() {

            }
        });
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        StatusBarUtil.setTranslucentForImageView(this, titleBar);
        etUsername = (MaterialEditText) findViewById(R.id.et_username);
        etNum = (MaterialEditText) findViewById(R.id.et_num);
        tvNum = (TextView) findViewById(R.id.tv_num);
        etPassword = (MaterialEditText) findViewById(R.id.et_password);
        etPassword2 = (MaterialEditText) findViewById(R.id.et_password2);
        etINviteCode = findViewById(R.id.et_invite);
        btRegister = findViewById(R.id.bt_register);
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {

        return R.layout.activity_register;
    }

    @Override
    public String getUsername() {
        return etUsername.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString().trim();
    }

    @Override
    public String getPassword2() {
        return etPassword2.getText().toString().trim();
    }

    @Override
    public String getNum() {
        return etNum.getText().toString().trim();
    }

    @Override
    public String getetINviteCode() {
        return etINviteCode.getText().toString().trim();
    }

    @Override
    public void Success() {
        setResult(1);
        startActivityAnim(new Intent(this, MainActivity.class));
        finishAnim();
    }

    @Override
    public void loginFail() {
    }


    @Override
    public void error(int code) {

        if (code == LOGIN_OR_PASSWORD_ERROR) {
            showToast("用户名密码错误！");
        } else if (code == BIND_EXISTS_ERROR) {
            showToast("已经注册错了！");
        } else if (code == LOGIN_ERROR) {
            showToast("登录错误！");
        } else {
            showToast("请求失败！");
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivityAnim(new Intent(RegisterActivity.this, LoginActivity.class));
        finish();
    }

    @Override
    public void showToast(String message) {
        ToastUtils.show(message);
    }

    @Override
    public void setNumcode(String s) {
        tvNum.setText(s);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_num:
                if (getPresenter() != null) {
                    getPresenter().setNUm();
                }
                break;
            case R.id.bt_register:
                if (getPresenter() != null) {
                    getPresenter().register();
                }

                break;
        }
    }
}
